package com.ld.admin.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.ld.admin.service.user.UserCommonService;
import com.ld.admin.util.daliySentence.DaliySentenceUtil;
import com.ld.admin.util.daliySentence.model.DaliySentenceSet;
import com.ld.common.util.CookieUtil;
import com.ld.shieldsb.common.composition.util.ConvertUtil;
import com.ld.shieldsb.common.composition.util.SecurityUtil;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.web.util.Web;

@Service
public class DaliySentenceService extends UserCommonService {
    private static final String DALIY_SENTENCE_SOURCE = "DaliySentenceSource";

    /**
     * 获取每日一句
     * 
     * @Title getDaliySentence
     * @author 吕凯
     * @date 2019年12月28日 上午10:56:44
     * @return Result
     */
    public Result getDaliySentence() {
        DaliySentenceSet setInfo = getDaliySentenceSourceSet();
        boolean enable = true;
        List<String> sourceSet = null;
        if (setInfo != null) {
            sourceSet = setInfo.getSelectKey();
            enable = setInfo.isEnable();
        }
        String content = "";
        if (enable) {
            content = DaliySentenceUtil.getRandomSentence(sourceSet);
        }
        Result result = new Result();
        Map<String, String> data = new HashMap<>();
        data.put("title", "每日一句"); // 标题
        result.setSuccess(true);
        result.setMessage(content);
        result.setData(data);
        return result;
    }

    public void getAllKeys(HttpServletRequest request) {
        DaliySentenceSet setInfo = getDaliySentenceSourceSet();
        List<String> allKeys = DaliySentenceUtil.getAllKeys();
        if (setInfo == null) {
            setInfo = new DaliySentenceSet();
            setInfo.setEnable(true);
            Map<String, Boolean> checkedMap = new HashMap<>();
            allKeys.forEach(key -> {
                checkedMap.put(key, true);
            });
            setInfo.setCheckedMap(checkedMap);
        }
        setInfo.setAllKeys(allKeys);
//        request.setAttribute("checkedMap", checkedMap);
        request.setAttribute("model", setInfo);
    }

    /**
     * 获取设置的数据源
     * 
     * @Title getDaliySentenceSourceSet
     * @author 吕凯
     * @date 2020年1月8日 下午5:09:59
     * @return List<String>
     */
    private DaliySentenceSet getDaliySentenceSourceSet() {
        DaliySentenceSet daliySentenceSourceSet = null;
        String cookieDaliySentenceSource = CookieUtil.getCookie(request, DALIY_SENTENCE_SOURCE + Web.Util.getSessionUserId(request));
        if (cookieDaliySentenceSource != null) {
            // 不支持中文需要转码
            cookieDaliySentenceSource = SecurityUtil.simpleDecrypt(cookieDaliySentenceSource);
            daliySentenceSourceSet = ConvertUtil.jsonStr2Obj(cookieDaliySentenceSource, DaliySentenceSet.class);
        }
        return daliySentenceSourceSet;
    }

    public Result setDaliySentenceSource(List<String> keys, boolean enabled) {
        if (keys == null) {
            return Web.Response.error("设置失败！数据源不能为空");
        }

        DaliySentenceSet setInfo = new DaliySentenceSet();
        setInfo.setEnable(enabled);
        List<String> allKeys = DaliySentenceUtil.getAllKeys();
        Map<String, Boolean> checkedMap = new HashMap<>();
        allKeys.forEach(key -> {
            if (keys.contains(key)) {
                checkedMap.put(key, true);
            } else {
                checkedMap.put(key, false);
            }
        });
        setInfo.setCheckedMap(checkedMap);

        String preference = ConvertUtil.obj2JsonStr(setInfo);
        // 不支持中文需要转码
        preference = SecurityUtil.simpleEncrypt(preference);

        CookieUtil.addCookie(response, 60 * 60 * 24 * 365 * 10, DALIY_SENTENCE_SOURCE + Web.Util.getSessionUserId(request), preference);
        return Web.Response.success("设置成功！");
    }

}
